package com.taxipark.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DriversServlet extends HttpServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String name = request.getParameter("password");
		String id = request.getParameter("id");

		Connection conn = null;
		Statement s = null;
		try {
			String userName = "root";
			String password = "root";
			String url = "jdbc:mysql://localhost:3306/db_taxidrivers";
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url, userName, password);
			System.out.println("Database connection established");

			conn.setAutoCommit(false);
			s = conn.createStatement();
			String query = "SELECT id FROM managers WHERE id='" + id + "' AND name='" + name + "';";
			ResultSet rs = s.executeQuery(query);

			if (!rs.next()) {

				Regestration regestration = new SimpleRegestration();
				String drivers = regestration.printAllDrivers();

				// set up response to client
				response.setContentType("text/html");
				PrintWriter out = response.getWriter();

				// start XHTML document
				out.println("<?xml version = \"1.0\"?>");

				out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD " + "XHTML 1.0 Strict//EN\" \"http://www.w3.org" + "/TR/xhtml1/DTD/xhtml1-strict.dtd\">");

				out.println("<html xmlns = \"http://www.w3.org/1999/xhtml\">");

				// head section of document
				out.println("<head>");

				out.println("<title>Thank you!</title>");
				out.println("</head>");

				out.println("<body>");

				// process results

				out.print(drivers);

				// end XHTML document
				out.println("</body></html>");
				out.close();

			}
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			System.err.println("Cannot connect to database server");
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e2) {
			}
		} finally {
			if (conn != null) {
				try {
					conn.close();
					System.out.println("Database connection terminated");
				} catch (Exception e) { /* ignore close errors */
				}
			}
		}

	} // end of doPost method

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Regestration regestration = new SimpleRegestration();
		// String drivers = regestration.printAllDrivers();

		// set up response to client
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		// start XHTML document
		out.println("<?xml version = \"1.0\"?>");

		out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD " + "XHTML 1.0 Strict//EN\" \"http://www.w3.org" + "/TR/xhtml1/DTD/xhtml1-strict.dtd\">");

		out.println("<html xmlns = \"http://www.w3.org/1999/xhtml\">");

		// head section of document
		out.println("<head>");

		out.println("<title>Thank you!</title>");
		out.println("</head>");

		out.println("<body>");

		// process results

		out.print("Get");

		// end XHTML document
		out.println("</body></html>");
		out.close();

	} // end of doPost method

}
